<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="referrer" content="no-referrer" />
    <title>Document</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <style>
        tbody>tr>td>img{
            width: 50px;
            height: 80px;
        }
        .count{
            width: 50px;
        }
    </style>
</head>

<body>
    <div class="container" id="app">

        <table class="table table-hover">
            <thead>
                <tr>
                    <th>全选:<input type="checkbox" v-model="allChecked" @change="changeAll"></th>
                    <th>商品</th>
                    <th>单价</th>
                    <th>数量</th>
                    <th>小计</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item of carts">
                    <td><input type="checkbox" v-model="item.isSelected" @change="handleItem"></td>
                    <td><img :src="item.productCover" alt=""></td>
                    <td>{{item.productPrice}}</td>
                    <td ><input  class="count" type="number" v-model.number="item.productCount"></td>
                    <td>￥{{item.productPrice*item.productCount|handleSum}}</td>
                    <td><button class="btn btn-danger">删除</button></td>
                </tr>
            </tbody>
        </table>
        <p>总计:￥{{sum()|handleSum}}</p>
    </div>
    <script>
        new Vue({
            el: "#app",
            data: {
                carts: [],
                allChecked:true
            },
            mounted() {
                $.ajax({
                    url: "http://47.108.197.28:8000/cart",
                    success: res => {
                        console.log(res);
                        this.carts = res
                    }
                })
            },
            filters:{
                handleSum(val){
                    return val.toFixed(2)
                }
            },
            methods: {
                sum(){
                    var carts = this.carts;
                    var sum =0;
                    carts.forEach(item=>{
                        if(item.isSelected){
                            sum+=(item.productPrice)*(item.productCount);
                        }
                    })
                    return sum;
                },
                changeAll(){
                    // console.log(this.allChecked);
                    this.carts.forEach(item=>item.isSelected=this.allChecked)
                },
                handleItem(){
                    this.allChecked = this.carts.every(item=>item.isSelected);
                }
            },
           
        })
    </script>
</body>

</html>